<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

            <el-form-item label="产品" prop="productIds">
                <el-select v-model="queryParams.productIds" placeholder="请选择产品" clearable size="small">
                    <el-option v-for="item in productOptions" :key="item.id" :label="item.modelDesLocal"
                        :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="工位" prop="shopIds">
                <treeselect v-model="queryParams.shopIds" searchable style="width: 240px" :options="shopOptions"
                    :normalizer="normalizerazShop" placeholder="请选择工位" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                    <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="工装编码" prop="frockCode">
                <el-input v-model="queryParams.frockCode" placeholder="请输入工装编码" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="工装名称" prop="frockName">
                <el-input v-model="queryParams.frockName" placeholder="请输入工装名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="厂家" prop="factory">
                <el-input v-model="queryParams.factory" placeholder="请输入厂家" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <!-- <el-form-item label="记录日期" prop="recordDate" label-width="118px">
                <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']">
                </el-date-picker>
            </el-form-item> -->
            <el-form-item>
                <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['frock:seat:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['frock:seat:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['frock:seat:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['frock:seat:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="seatList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="工装主键id" align="center" prop="frockId" v-if="false" />
            <el-table-column label="工装编码" width="200" align="center" prop="frockCode" />
            <el-table-column label="工装名称" align="center" prop="frockName" />
            <el-table-column label="工装规格" align="center" prop="frockSpes" />
            <el-table-column label="厂家" align="center" prop="factory" />
            <el-table-column label="尺寸" align="center" prop="size" />
            <!-- <el-table-column label="记录日期" align="center" prop="recordDate" /> -->
            <el-table-column label="产品" align="center" prop="productIds" width="200" :formatter="productFormatter" />
            <el-table-column label="工位" align="center" width="300" prop="shopNames" :formatter="shopFormatter" />
            <el-table-column label="设定寿命时长" align="center" width="100" prop="life" />
            <el-table-column label="设定次数" align="center" prop="number" />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <span>{{scope.row.status===1?'新建':scope.row.status===2?'已绑定':scope.row.status===3?'已解绑':scope.row.status===4?'报废':''}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['frock:seat:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['frock:seat:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改工装管理基础对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="产品" prop="productIds">
                            <el-select style="width: 600px;" multiple v-model="form.productIds" filterable
                                placeholder="请选择产品" clearable size="small">
                                <el-option v-for="item in productOptions" :key="item.id" :label="item.modelDesLocal"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="工位" prop="shopIds">
                            <treeselect style="width: 600px;" multiple v-model="form.shopIds" clearable
                                :options="shopOptions" :normalizer="normalizerazShop" placeholder="请选择工位" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">

                    <el-col :span="12">
                        <el-form-item label="工装编码" prop="frockCode">
                            <el-input v-model="form.frockCode" placeholder="请输入工装编码" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="工装名称" prop="frockName">
                            <el-input v-model="form.frockName" placeholder="请输入工装名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="工装规格" prop="frockSpes">
                            <el-input v-model="form.frockSpes" placeholder="请输入工装规格" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="厂家" prop="factory">
                            <el-input v-model="form.factory" placeholder="请输入厂家" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="尺寸" prop="size">
                            <el-input v-model="form.size" placeholder="请输入尺寸" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="设定次数" prop="number">
                            <el-input v-model="form.number" placeholder="请输入设定次数" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="设定寿命时长" prop="life" label-width="98px">
                            <el-input v-model="form.life" placeholder="请输入设定寿命时长" />
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { listSeat, getSeat, delSeat, addSeat, updateSeat, exportSeat } from "@/api/equipment/toolingList";
    import { listSetModel } from "@/api/parameter/set_model";
    import { treeselect } from "@/api/base/shopConfig";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
        name: "ToolingList",
        components: { Treeselect },
        data() {
            return {
                // 遮罩层
                loading: true, statusOptions: [{ value: 1, label: '新建' }, { value: 2, label: '已绑定' }, { value: 3, label: '已解绑' }, { value: 4, label: '报废' },],
                // 选中数组
                ids: [], dateRange: [], productOptions: [], shopOptions: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 工装管理基础表格数据
                seatList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                }
            };
        },
        created() {
            this.getList();
            listSetModel().then(res => { this.productOptions = res.rows });
            treeselect().then((response) => {
                this.shopOptions = response.data;
            });
        },
        methods: {
            /** 查询工装管理基础列表 */
            getList() {
                this.loading = true;
                listSeat(this.queryParams).then(response => {
                    this.seatList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            normalizerazShop(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.id,
                    label: node.label,
                    children: node.children
                };
            },
            productFormatter(row) {
                var arr = []
                this.productOptions.map(item => {
                    row.productIds.map(it => {
                        if (item.id === it) {
                            arr.push(item.modelDesLocal)
                        }
                    })
                })
                return arr.join('、')
            },
            shopFormatter(row) {
                return row.shopNames.join('、')
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    frockId: null,
                    frockCode: null,
                    frockName: null,
                    frockSpes: null,
                    factory: null,
                    size: null,
                    recordDate: null,
                    productId: null,
                    shopId: null,
                    life: null,
                    number: null,
                    status: 0,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.frockId)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加工装管理基础";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const frockId = row.frockId || this.ids
                getSeat(frockId).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改工装管理基础";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.frockId != null) {
                            updateSeat(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        } else {
                            addSeat(this.form).then(response => {
                                if (response.code === 200) {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const frockIds = row.frockId || this.ids;
                this.$confirm('是否确认删除工装管理基础编号为"' + frockIds + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delSeat(frockIds);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                }).catch(function () { });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有工装管理基础数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return exportSeat(queryParams);
                }).then(response => {
                    this.download(response.msg);
                }).catch(function () { });
            }
        }
    };
</script>